#include "home_page.h"
#include "ui_home_page.h"

// 初始化音乐
void Home_Page::my_music()
{
    songList << "http://music.163.com/song/media/outer/url?id=1815969317.mp3"  // 最美的瞬间
             << "http://music.163.com/song/media/outer/url?id=1406036590.mp3"  // 须尽欢
             << "http://music.163.com/song/media/outer/url?id=1457476994.mp3"  // 闲庭絮
             << "http://music.163.com/song/media/outer/url?id=2648336973.mp3";  // 春庭雪

    // 初始化音频对象
    mediaPlayer = new QMediaPlayer(this);

    // 初始化音频
    Media_Url.setUrl("http://music.163.com/song/media/outer/url?id=1815969317.mp3");
    mediaPlayer->setMedia(Media_Url);

    Media_max = mediaPlayer->duration();

    connect(mediaPlayer , &QMediaPlayer::durationChanged , this , [=](qint64 duration){
        // qDebug() << duration / 1000;
        Media_max = duration / 100;
        // 设置进度条
        ui->progressSlider->setMaximum(Media_max);
    });

    // 定时器
    Media_time  = new QTimer;

    connect( Media_time , &QTimer::timeout , this , [&](){
         ui->progressSlider->setValue(this->mediaPlayer->position() / 100);
    });

    // 设置音量条的初始值
    ui->volumeSlider->setValue(100);
    // ui->dial->setValue(100);

    playbackMode = Sequential;    // 默认顺序播放

    // 初始化 歌曲列表
    ui->musicListWidget->setViewMode(QListView::ListMode); // 设置为列表模式
    ui->musicListWidget->setAlternatingRowColors(true);


    // 在列表中添加歌曲
    this->Media_Item = new  QListWidgetItem(QIcon(":/img_1.jpg") , "最美的瞬间");
    ui->musicListWidget->addItem(this->Media_Item);

    this->Media_Item = new  QListWidgetItem(QIcon(":/img_2.jpg") , "须尽欢");
    ui->musicListWidget->addItem(this->Media_Item);

    this->Media_Item = new  QListWidgetItem(QIcon(":/img_3.jpg") , "闲庭絮");
    ui->musicListWidget->addItem(this->Media_Item);

    this->Media_Item = new  QListWidgetItem(QIcon(":/img_4.jpg") , "DJ光头-春庭雪");
    ui->musicListWidget->addItem(this->Media_Item);

    // 当用户切换 则便跟歌曲
    connect(ui->musicListWidget , &QListWidget::currentRowChanged , this , [&](int currentRow){
        QString url_str = "http://music.163.com/song/media/outer/url?id=";

        // qDebug() << currentRow;

        switch (currentRow) {
            case 0: url_str += "1815969317"; break;
            case 1: url_str += "1406036590"; break;
            case 2: url_str += "1457476994"; break;
            case 3: url_str += "2648336973"; break;
        }
        url_str += ".mp3";

        // 切换歌曲
        this->mediaPlayer->setMedia(QUrl(url_str));
        // 播放歌曲
        mediaPlayer->play();
        // 更新播放按钮图标为播放状态
        // ui->pushButton_start->setIcon(QIcon(":/img/img_12.png"));
    });
}

// 开始与暂停
void Home_Page::on_pushButton_start_clicked()
{
    if(mediaPlayer->state() == QMediaPlayer::StoppedState)
    {
        ui->pushButton_start->setIcon(QIcon(":/img/img_13.png"));
        mediaPlayer->play();
        Media_time->start(500);
    }
    else if (mediaPlayer->state() == QMediaPlayer::PlayingState) {
        ui->pushButton_start->setIcon(QIcon(":/img/img_12.png"));
        mediaPlayer->pause();
        Media_time->stop();
    }
    else if (mediaPlayer->state() == QMediaPlayer::PausedState) {
        ui->pushButton_start->setIcon(QIcon(":/img/img_13.png"));
        mediaPlayer->play();
        Media_time->start(500);
    }
}

// 上一首
void Home_Page::on_prevButton_clicked()
{
    if (songList.isEmpty()) {
        QMessageBox::warning(this, "提示", "播放列表为空！", QMessageBox::Ok);
        return;
    }

    // 切换到上一首歌曲
    currentSongIndex = (currentSongIndex - 1 + songList.size()) % songList.size();
    QString prevSong = songList[currentSongIndex];

    mediaPlayer->setMedia(QUrl(prevSong));
    mediaPlayer->play();

    // 更新列表选中状态
    ui->musicListWidget->setCurrentRow(currentSongIndex);
}

// 下一首
void Home_Page::on_nextButton_clicked()
{
    if (songList.isEmpty()) {
        QMessageBox::warning(this, "提示", "播放列表为空！", QMessageBox::Ok);
        return;
    }

    // 切换到下一首歌曲
    currentSongIndex = (currentSongIndex + 1 + songList.size()) % songList.size();
    QString prevSong = songList[currentSongIndex];

    mediaPlayer->setMedia(QUrl(prevSong));
    mediaPlayer->play();

    // 更新列表选中状态
    ui->musicListWidget->setCurrentRow(currentSongIndex);
}

// 音量
void Home_Page::on_volumeSlider_valueChanged(int value)
{
    this->mediaPlayer->setVolume(ui->volumeSlider->value());
}

// 进度条
void Home_Page::on_progressSlider_valueChanged(int value)
{
    // 设置播放进度
    this->mediaPlayer->setPosition(ui->progressSlider->value() * 100);
}

void Home_Page::setPlaybackMode(Home_Page::PlaybackMode mode)
{
    playbackMode = mode;
    // 根据不同的播放模式，更新界面显示或其他相关操作
    updatePlaybackModeUI();
}

void Home_Page::updatePlaybackModeUI()
{
    switch (playbackMode) {
    case Sequential:
        ui->playbackModeComboBox->setCurrentText("顺序播放");
        break;
    case Random:
        ui->playbackModeComboBox->setCurrentText("随机播放");
        break;
    case Loop:
        ui->playbackModeComboBox->setCurrentText("循环播放");
        break;
    }
}

// 播放模式
void Home_Page::on_playbackModeComboBox_currentIndexChanged(int index)
{
    switch (index) {
    case 0:
        setPlaybackMode(Sequential);
        break;
    case 1:
        setPlaybackMode(Random);
        break;
    case 2:
        setPlaybackMode(Loop);
        break;
    }
}


// 增加音乐
void Home_Page::on_addMusicButton_clicked()
{
    // 打开文件对话框，让用户选择音乐所在的目录
    QString path = QFileDialog::getExistingDirectory(this, "请选择音乐所在的目录", "D:\\project\\music");
    // 根据这个路径，获取里面的所有的mp3、wav文件
    QDir dir(path);
    QStringList musicList = dir.entryList(QStringList() << "*.mp3" << "*.wav");
    ui->musicListWidget->addItems(musicList);
}
